<template>
  <el-dialog title="分配角色" :visible="isShowRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <!-- 分配角色 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{
          item.name
        }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleListAPI } from '@/api/setting'
import { getUserDetailByIdAPI } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    isShowRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 所有角色数组
      roleList: [],
      // 选中的数组
      checkList: [],
      // 当前正在编辑的用户id
      userId: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    //  获取所有角色
    async getRoleList() {
      const { rows } = await getRoleListAPI({
        page: 1,
        pagesize: 999
      })
      this.roleList = rows
    },
    async getUserDetail(id) {
      const { roleIds } = await getUserDetailByIdAPI(id)
      this.checkList = roleIds // 赋值本用户的角色
      // 顺便把 id 记下来, 后面点击确定的时候发请求需要
      this.userId = id
    },
    async btnOK() {
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('修改成功')
      this.$emit('closeRoleDialog')
    },
    btnCancel() {
      // 清理数据
      this.checkList = []
      this.$emit('closeRoleDialog')
    }
  }
}
</script>

<style>

</style>
